<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\my\controller;

use app\admin\model\ChangeCoinModel;
use cmf\controller\RestBaseController;
use app\admin\model\SignSetModel;
use api\index\controller\CommonController;
use app\admin\model\MemberModel;
use think\Db;
use think\Request;
use DateTime;

class SignSetController extends RestBaseController
{
    protected $noNeedLogin = [];


    /**
     * @param SignSetModel $signSetModel
     * 点击签到
     */
    public function sign(SignSetModel $signSetModel)
    {
        if (! $this->request->isGet()){
            $this->success('请求方式错误');
        }
        //最近的签到数据
        $recentlySign = CommonController::findWhereData('sign_record',['menber_id'=>$this->getUserId()],'','create_time desc');
        //判断是否签到
        if (! $recentlySign ){//如果没有签到
            $this->saveSign(1);
        }
        $days = CommonController::interval_day($this->getUserId());

        //判断今天是否签到过了
        if ($days == 0 ){
            $this->error('您今天已经签到过了');
        }
        //判断签到隔断是否超过一天或者已经签满30天
        if ($days > 1 || $recentlySign['con_coin'] == 30){
            $this->saveSign(1);
        }
        $this->saveSign($recentlySign['con_coin'] +1);
    }

    /**
     * @param $day
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 根据天数返回对应的球币
     */
    public function getCoin($day)
    {
        if(! $sign = SignSetModel::where('day',$day)->find()){
            $this->error('没有找到对应天数设置');
        }
        return $sign->get_coin;
    }



    /**
     * @param $day
     * 保存数据
     */
    public function saveSign($day)
    {
        $member =  MemberModel::get($this->userId);
        Db::startTrans();
        try {
            //签到记录插入一条数据
            $signIn = $member->signIn()->save(['con_coin' => $day]);
            //球币变动插入数据
            CommonController::changeCoin($this->getUserId(),ChangeCoinModel::SIGN_IN,$signIn->id,'签到', '+'.$this->getCoin($day),$this->getCoin($day) + $member->coin);
            //修改用户表球币
            $member->setInc('coin',$this->getCoin($day));
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error('签到失败');
        }
        $this->success('签到成功');
    }

    /**
     * 签到列表
     */
    public function signList($limit=0)
    {
        //最近的签到数据
        $recentlySign = CommonController::findWhereData('sign_record',['menber_id'=>$this->getUserId()],'','create_time desc');
        $day = 0;
        if ($recentlySign){
            $days = CommonController::interval_day($this->getUserId());
            //连续签到天数，过了30天从0开始
            if ($days <= 1 || $recentlySign['con_coin'] !== 30){
                $day = $recentlySign['con_coin'];
            }
            $sign = SignSetModel::field('id,day,get_coin')->limit($limit)->select()->each(function($item,$key) use($day){
                $item['is_sign'] = $key < $day? 1 : 0;
            });
        }else{
            $sign = SignSetModel::field('id,day,get_coin')->limit($limit)->select()->each(function($item){
                $item['is_sign'] =  0;
            });
        }

        $this->success('成功',['sign'=>$sign,'days'=>$day] );
    }

}
